<%@ page import="com.interfaces.TravelAgentController" %>
<%@ page import="com.core.sysDict.SysDictJsp" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<!--
This is a starter template page. Use this page to start your new project from
scratch. This page gets rid of all links and provides the needed markup only.
-->
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>旅行社信息</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">

    <%
    	String path = request.getContextPath();
            path = "/".equals(path)?"":path;
            request.setAttribute("path", path);
            String sysDictJson=SysDictJsp.getSysDictJson(session);
    %>
    <script type="text/javascript">
        var path = "${path}";
    </script>

    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="${path}/public/adminlte/bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="${path}/public/font-awesome-4.7.0/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="${path}/public/ionicons/ionicons.min.css">
    <!-- DataTables -->
    <link rel="stylesheet" href="${path}/public/adminlte/plugins/datatables/dataTables.bootstrap.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="${path}/public/adminlte/dist/css/AdminLTE.min.css">
    <!-- AdminLTE Skins. We have chosen the skin-blue for this starter
          page. However, you can choose any other skin. Make sure you
          apply the skin class to the body tag so the changes take effect.
    -->
    <link rel="stylesheet" href="${path}/public/adminlte/dist/css/skins/_all-skins.min.css">

    <link rel="stylesheet" href="${path}/css/common.css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script type="text/javascript" src="${path}/js/json2.js"></script>
</head>
<!--
BODY TAG OPTIONS:
=================
Apply one or more of the following classes to get the
desired effect
|---------------------------------------------------------|
| SKINS         | skin-blue                               |
|               | skin-black                              |
|               | skin-purple                             |
|               | skin-yellow                             |
|               | skin-red                                |
|               | skin-green                              |
|---------------------------------------------------------|
|LAYOUT OPTIONS | fixed                                   |
|               | layout-boxed                            |
|               | layout-top-nav                          |
|               | sidebar-collapse                        |
|               | sidebar-mini                            |
|---------------------------------------------------------|
-->
<body >
<div class="container">
            <div class="row">
                 <div class="col-md-4">
                    <div class="form-group">
                        <label for="travelAgentName">旅行社机构名</label>
                        <input type="text" class="form-control" id="travelAgentName" validata="len" errormsg="" emptyerrormsg="旅行社机构名称不能为空" empty="false" limitLength="6">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label for="phone">联系电话</label>
                        <input type="text" class="form-control" id="phone" validata="len" errormsg="" emptyerrormsg="电话号码不能为空" empty="false" limitLength="30">
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label for="remark">备注</label>
                        <input type="text" class="form-control" id="remark" validata="len" errormsg="" emptyerrormsg="备注不能为空" empty="false" limitLength="6">
                    </div>
                </div>
            </div>
            <div class="row">
                    <table id="table" class="table table-striped table-bordered" cellspacing="0" width="100%">
					</table>
			</div>
            <div class="row">
                <div class="col-md-12">
                	<button type="button" class="btn btn-primary" id=add-btn>添加送签社</button>
                    <button type="submit" class="btn btn-primary" id="btnAdd">保 存</button>
                </div>
            </div>
<!-- ./wrapper -->
</div>
<!-- REQUIRED JS SCRIPTS -->

<!-- jQuery 2.2.3 -->
<script src="${path}/public/adminlte/plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="${path}/public/adminlte/bootstrap/js/bootstrap.min.js"></script>
<!-- DataTables -->
<script src="${path}/public/adminlte/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="${path}/public/adminlte/plugins/datatables/dataTables.bootstrap.min.js"></script>
<!-- SlimScroll -->
<script src="${path}/public/adminlte/plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script src="${path}/public/adminlte/plugins/fastclick/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="${path}/public/adminlte/dist/js/app.min.js"></script>
<!-- 判空和输入检测 -->
<script src="${path}/js/validata.js"></script>

<script src="${path}/js/sysDict.js"></script>
<!-- Optionally, you can add Slimscroll and FastClick plugins.
     Both of these plugins are recommended to enhance the
     user experience. Slimscroll is required when using the
     fixed layout. -->
<!-- page script -->
<script>
function formatRepo (repo) {
      if (repo.loading) {return repo.text;}
      return repo.text+"("+repo.id+")";
    }
    function formatRepoSelection (repo) {
    return repo.id ; 
    }
    var id = (new Date()).valueOf();
    var table;
$(function () {
     table = $('#table').DataTable({
       		"lengthChange": false,
            "searching": false,
            "paging": false,
            "ordering": false,
           	"ajax": {
               "url": "${path}<%=TravelAgentController.URL_INTERVAL%>",
               
               "type": "post",
               "error":function(){
              
               },
              
           },
           "columns": [
               { "data": "sendAgentCode", "title":"送签社代码","defaultContent":""},
               { "data": "sendAgentName", "title":"送签社名字","defaultContent":""}
           ],
           "language": {
               "sProcessing": "处理中...",
               "sZeroRecords": "没有匹配结果",
               "sInfo": " ",
               "sInfoEmpty": " ",
               "sInfoFiltered": " ",
               "sInfoPostFix": "",
               "sUrl": "",
               "sEmptyTable": "区间设置条数为空",
               "sLoadingRecords": "载入中...",
               "sInfoThousands": ",",
               "oPaginate": {
                   "sFirst": "首页",
                   "sPrevious": "上页",
                   "sNext": "下页",
                   "sLast": "末页"
               },
               "oAria": {
                   "sSortAscending": ": 以升序排列此列",
                   "sSortDescending": ": 以降序排列此列"
               }
           }
           
       });
       //批量编辑
       function edit(){
	       table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
	       var tds=$(this.node()).children();
	       var id=this.index();
           $.each(tds, function(i,val){
               var jqob=$(val);
               var txt=jqob.text();
               var put=$("<input type='text' onchange='clickID("+id+")' id='id"+id+"_"+i+"'>");
               put.val(txt);
               jqob.html(put);
           });
          });
          //刷新列表
          table.draw(false);
           //$(".edit-btn").click();
       }; 
       $("#add-btn").click(function(){
          var row;
	         row=table.row.add( {
		        "sendAgentCode":"",
		        "sendAgentName":"",
	    		} ).draw(false);
    	//添加一行,将每一行的td内容修改成input
    	var tds=$(row.node()).children();
           $.each(tds, function(i,val){
               var jqob=$(val);
               		var txt=jqob.text();
               		var put=$("<input type='text' onchange='clickID("+row.index()+")' id='id"+row.index()+"_"+i+"'>");
               		//if(i==0&&row.index()!=0) put=$("<input type='text' readonly='readonly' onchange='clickID("+row.index()+")' id='id"+row.index()+"_"+i+"'>");
               		put.val(txt);
               		jqob.html(put);
           });
       });
     });
     $("#btnAdd").click(function () {
     if(table.rows().data().length<=0){
     		alert("请至少添加一组送签社信息设置再保存！");
     		return false;
       	}
     	
        var travelAgentName = $("#travelAgentName").val();
         if(travelAgentName == ""){
            alert("请输入旅行社名称");
            return; 
        }
        var phone = $("#phone").val();
         if(phone == ""){
            alert("请输入联系方式");
            return; 
        }
        var remark = $("#remark").val();
        var flag=0;
        
      	table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
      		if(flag==1) return false;
      		var id=this.index();
      		var tds=$(this.node()).children();
          	$.each(tds, function(i,val){
              var jqob=$(val);
          	   var txt = $("#id"+id+"_"+i).val();
          	   var res=checkData(txt,id,i);
          	   if(res!='0'){
          	   	flag=1;
                alert(res);
                $("#id"+id+"_"+i).focus();
                return false;
          	   }
            });
		 });
		 if(flag==1) return false;
        $.ajax({
            url:"${path}<%=TravelAgentController.URL_ADD%>",
            type:"post",
            data:{
                id:id,
                travelAgentName:travelAgentName,
                phone:phone,
				remark:remark
            },
            success:function(result){
                var re = JSON.parse(result);
                if(re.returnCode=="0"){
                	$.ajax({
            			url:"${path}<%=TravelAgentController.URL_ADDRELATION%>",
            			type:"post",
            			data:{
                			travelAgentId:id,
                			sendAgentName:sendAgentName,
							sendAgentCode:sendAgentCode
            			},
            			success:function(result){
                			var ress = JSON.parse(result);
                			if(ress.returnCode=="0"){
                			alert("新增旅行社信息成功！");
                			var url= window.location.search;
                			var arr = url.split('&');
                			parent.refresh(arr[arr.length-1]);
                    		parent.closeAndActiveTab(arr[arr.length-2],arr[arr.length-1]);
                			}else{
                    			alert(ress.msg);
                			}
            			},
            			error:function(request) {      // 设置表单提交出错
                			alert(request);  //登录错误提示信息
            			}
        				});
                }else{
                    alert(re.msg);
                }
            },
            error:function(request) {      // 设置表单提交出错
                alert(request);  //登录错误提示信息
            }
        });
        return false;
     });
</script>
</body>
</html>
